import axios from "axios";
import { oldmanLoginRequest, oldmanCommonRequest } from "./base";

interface LoginProps {
  username: string;
  password: string;
}

export const login = async (data: LoginProps) => {
  const formData = new URLSearchParams();

  formData.append("grant_type", "password"); // 文件
  formData.append("client_id", "client-app");
  formData.append("client_secret", "123456");
  formData.append("username", data.username);
  formData.append("password", data.password);

  const config = {
    headers: {
      "Content-Type": "application/x-www-form-urlencoded", // 让axios自动设置Content-Type为multipart/form-data
    },
  };

  const res = await axios.post(
    `${import.meta.env.VITE_APP_OLDMAN_API_BASE_URL}/auth/oauth/token`,
    formData,
    config
  );

  return res;
};

interface RegisterProps {
  userName: string;
  password: string;
  location: string;
  nickName: string;
  status: "admin" | "common"; // admin common
  role: 0 | 1; // 0表示普通用户，1表示专家
}

export const register = async (data: RegisterProps) => {
  const res = await oldmanLoginRequest.post("/provider/provider/addUser", data);
  return res;
};

export const getCommunityArticleList = async (data) => {
  const res = await oldmanCommonRequest.post(
    "/provider/Community/getList",
    data
  );
  return res;
};

//修改头像
export const postHeadImg = async (data) => {
  const res = await oldmanCommonRequest.post("/provider/provider/load", data);
  return res;
};
//获取行程
export const getMyWay = async () => {
  const res = await oldmanCommonRequest.post("/provider/journey/getList", {});
  return res;
};
//用户资讯接口
export const getUserAsk = async (data) => {
  const res = await oldmanCommonRequest.post(
    "/provider/informationSpecialist/addInformationSpecialist",
    data
  );
  return res;
};
//获取资讯列表
export const getAskList = async (data) => {
  const res = await oldmanCommonRequest.post(
    "/provider/informationSpecialist/getList",
    data
  );
  return res;
};
//专家回复接口
export const getHighAsk = async (data) => {
  const res = await oldmanCommonRequest.post(
    "/provider/informationSpecialist/replyInformationSpecialist",
    data
  );
  return res;
};
//完成行程
export const finishWay = async (data) => {
  const res = await oldmanCommonRequest.post(
    "/provider/journey/finishJourney",
    data
  );
  return res;
};
//完成行程
export const addWay = async (data) => {
  const res = await oldmanCommonRequest.post(
    "/provider/journey/addJourney",
    data
  );
  return res;
};
//获取紧急救助信息
export const getHelp = async () => {
  const res = await oldmanCommonRequest.post("/provider/journey/getHelp", {});
  return res;
};
//选择救助人
export const choosePerson = async (data) => {
  const res = await oldmanCommonRequest.post("/provider/journey/help", data);
  return res;
};
// 附近的人
export const nearPerson = async () => {
  const res = await oldmanCommonRequest.post(
    "/provider/provider/getPeopleNearby",
    {}
  );
  return res;
};
// 添加好友
export const addPerson = async (data) => {
  const res = await oldmanCommonRequest.post(
    "/provider/provider/addFriendById",
    data
  );
  return res;
};
//获取全量救助历史记录
export const getAllHelp = async () => {
  const res = await oldmanCommonRequest.post(
    "/provider/journey/getHelpHistory",
    {}
  );
  return res;
};

interface publishCommunityArticleProps {
  content: string;
  type: string;
}

export const publishCommunityArticle = async (
  data: publishCommunityArticleProps
) => {
  const res = await oldmanCommonRequest.post(
    "/provider/Community/addCommunity",
    data
  );
  return res;
};

interface likeCommunityArticleProps {
  id: string;
}

export const likeCommunityArticle = async (data: likeCommunityArticleProps) => {
  const res = await oldmanCommonRequest.post("/provider/Community/Likes", data);
  return res;
};

export const getHealthInfo = async () => {
  const res = await oldmanCommonRequest.post("/provider/healthIndex/getOne");
  return res;
};

interface ReplyCommunityArticleProps {
  communityId: number; //社区文章id
  content: string;
}

export const replyCommunityArticle = async (
  data: ReplyCommunityArticleProps
) => {
  const res = await oldmanCommonRequest.post(
    "/provider/Community/replyCommunity",
    data
  );

  return res;
};

interface GetAllCommentsByIdProps {
  id: number;
}

export const getAllCommentsById = async (data: GetAllCommentsByIdProps) => {
  const res = await oldmanCommonRequest.post(
    "/provider/Community/getReply",
    data
  );
  return res;
};

interface GetUserInfoByIdProps {
  id: number;
}

export const getUserInfoById = async (data: GetUserInfoByIdProps) => {
  const res = await oldmanCommonRequest.post(
    "/provider/provider/selectUser",
    data
  );
  return res;
};

interface AddFriendByIdProps {
  id: number;
}

export const addFriendById = async (data: AddFriendByIdProps) => {
  const res = await oldmanCommonRequest.post(
    "/provider/provider/addFriendById",
    data
  );
  return res;
};

interface GetFriendByIdProps {
  id: number;
}

export const getFriendById = async (data: GetFriendByIdProps) => {
  const res = await oldmanCommonRequest.post(
    "/provider/provider/getFriendById",
    data
  );
  return res;
};

export const getPeopleNearby = async () => {
  const res = await oldmanCommonRequest.post(
    "/provider/provider/getPeopleNearby"
  );
  return res;
};
// 查询护工信息
export const getHugong = async () => {
  const res = await oldmanCommonRequest.post("/provider/nurse/getVisibleList");
  return res;
};
// 查询自己护工信息
export const getMyHugong = async (data) => {
  const res = await oldmanCommonRequest.post("/provider/nurse/addNurse", data);
  return res;
};
// 雇佣和解除护工信息
export const getHugongExit = async (data) => {
  const res = await oldmanCommonRequest.post(
    "/provider/nurse/hiredNurse",
    data
  );
  return res;
};

export const getActivityList = async () => {
  const res = await oldmanCommonRequest.post("/provider/activity/getList");
  return res;
};

interface addActivityProps {
  activityName: string;
  activityDescribe: string;
  deadline: Date;
  location: string;
}

export const addActivity = async (data: addActivityProps) => {
  const res = await oldmanCommonRequest.post(
    "/provider/activity/addActivity",
    data
  );
  return res;
};

interface enterActivityProps {
  id: number;
}

export const enterActivity = async (data: enterActivityProps) => {
  const res = await oldmanCommonRequest.post(
    "/provider/activity/enterActivity",
    data
  );
  return res;
};

export const enteredActivity = async () => {
  const res = await oldmanCommonRequest.post(
    "/provider/activity/MyAddActivity"
  );
  return res;
};

interface ExitActivityProps {
  id: string;
}
// 删除活动
export const exitActivity = async (data: ExitActivityProps) => {
  const res = await oldmanCommonRequest.post(
    "/provider/activity/deleteActivity",
    data
  );
  return res;
};
// 退出活动
export const exitActivity2 = async (data: ExitActivityProps) => {
  const res = await oldmanCommonRequest.post(
    "/provider/activity/exitActivity",
    data
  );
  return res;
};
// 添加文章
export const addNewArtical = async (data) => {
  const res = await oldmanCommonRequest.post(
    "/provider/Community/addCommunity",
    data
  );
  return res;
};
// 更新数据
export const updateListHelth = async () => {
  const res = await oldmanCommonRequest.post(
    "/provider/healthIndex/Random",
    {}
  );
  return res;
};
// 推荐文章
export const tuijianArtical = async () => {
  const res = await oldmanCommonRequest.post(
    "/provider/Community/selectCommunityRecommend",
    {}
  );
  return res;
};

// interface GetMedicineProps {
//   health: 0 | 1;
// }

export const getMedicine = async () => {
  const res = await oldmanCommonRequest.post(
    "/provider/medicine/getMedicine",
    {}
  );
  return res;
};

interface AddMedicineProps {
  medicineName: string;
  medicineNumber: number;
  month: number;
  // "2024-05-06 17:06:47";
}

export const addMedicine = async (data: AddMedicineProps) => {
  const res = await oldmanCommonRequest.post(
    "/provider/medicine/StockIn",
    data
  );
  return res;
};

interface RemoveMedicineProps {
  medicineName: string;
  medicineNumber: number;
}

export const removeMedicine = async (data: RemoveMedicineProps) => {
  const res = await oldmanCommonRequest.post(
    "/provider/medicine/StockOut",
    data
  );
  return res;
};

export const getMedicineHistory = async () => {
  const res = await oldmanCommonRequest.post(
    "/provider/medicine/SelectMedicineStockHistory",
    {}
  );
  return res;
};

export const getListOfoldpeople = async (data) => {
  const res = await oldmanCommonRequest.post(
    "/provider/RestHome/getList",
    data
  );
  return res;
};

// 新建养老院
export const getAddNewOld = async (data) => {
  const res = await oldmanCommonRequest.post(
    "/provider/RestHome/addRestHome",
    data
  );
  return res;
};
// 加入养老院
export const getJoinOld = async (data) => {
  const res = await oldmanCommonRequest.post(
    "/provider/RestHome/enterRestHome",
    data
  );
  return res;
};
// 退出养老院
export const getExitOld = async (data) => {
  const res = await oldmanCommonRequest.post(
    "/provider/RestHome/exitRestHome",
    data
  );
  return res;
};
// 已加入的养老院
export const getNowOld = async () => {
  const res = await oldmanCommonRequest.post(
    "/provider/RestHome/getUserRestHome",
    {}
  );
  return res;
};

export const handleHelp = async (id) => {
  const res = await oldmanCommonRequest.post("/provider/journey/help", { id });
  return res;
};

export const getHelpHistory = async () => {
  const res = await oldmanCommonRequest.post(
    "/provider/journey/getHelpHistory"
  );
  return res;
};

export const getUserInfo = async () => {
  const res = await oldmanCommonRequest.post("/provider/provider/getHttpUser");
  return res;
};

export const getYangLaoYuanCreateByMe = async () => {
  const res = await oldmanCommonRequest.post(
    "/provider/RestHome/getMyRestHome"
  );
  return res;
};

export const getMyIntervieweeOldHelper = async () => {
  const res = await oldmanCommonRequest.post("/provider/nurse/getMeHired");
  return res;
};

export const getMyInfo = async () => {
  const res = await oldmanCommonRequest.post("/provider/nurse/getMe");
  return res;
};

export const fireNurse = async (id: number) => {
  const res = await oldmanCommonRequest.post("/provider/nurse/hiredNurse", {
    id,
  });
  return res;
};

export const getNewHelp = async () => {
  const res = await oldmanCommonRequest.post("/provider/journey/mockHelp");
  return res;
};
